import helliker.id3.ID3v2FormatException;

import helliker.id3.MP3File;
import helliker.id3.Playlist;
import helliker.id3.PlaylistException;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.PrefetchCompleteEvent;
import javax.media.RealizeCompleteEvent;
import javax.media.Time;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.filechooser.FileFilter;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Point;
import java.awt.dnd.DropTargetListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.PrefetchCompleteEvent;
import javax.media.RealizeCompleteEvent;
import javax.media.Time;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
import javax.swing.filechooser.FileFilter;
import helliker.id3.ID3v2FormatException;
import helliker.id3.PlaylistException;
import helliker.id3.MP3File;
import helliker.id3.Playlist;

import javax.media.*;

//import JavaPlayer.PlaylistFilter;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;

import java.util.Random;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.BooleanControl;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;


public class CSPlayer extends javax.swing.JFrame implements ControllerListener, Runnable { 
	private Playlist playlist;  						//public class Playlist extends LinkedList
	private Player player = null;					//import javax.media.Player;
	private JPanel mainPanel = null;				//import javax.swing.JPanel;
	private String fileTitle = "";
	private String fileArtist = "";
	private String fileAlbum = "";
	private Thread playThread = null;
	Boolean s=true;				//for checking the sound on or off
    Boolean r=false;			//for checking the repeat on or off
    Boolean sh=false;			//for checking the shuffle on or off
    Random random = new Random();
  
    public CSPlayer() { 
        initComponents();
        setIconImage(new ImageIcon(".\\src\\icons\\Logo.jpg").getImage());
        playlist = new Playlist();
        Setting.loadSettings();
		File playlistFile = new File(Setting.getPlaylistDirectory(),	Setting.getPlaylistFile());
		if(playlistFile.exists() && playlistFile.isFile()) {
			duration.setString("Loading Playlist...");
			try {
				playlist.loadFromFile(playlistFile);
			}
			catch(IOException ex) {
				errorMessage(ex.getMessage());
			}
			catch(PlaylistException ex) {
				errorMessage(ex.getMessage());
			}
			duration.setString("");
		}
    } 
 
    @SuppressWarnings("unchecked") 
 
    private void initComponents() { 
 
    	jLayeredPane1 = new javax.swing.JLayeredPane();
        duration = new javax.swing.JProgressBar();
        volumeslider = new javax.swing.JSlider();
        pausebutton = new javax.swing.JButton();
        playbutton = new javax.swing.JButton();
        fastforwardbutton = new javax.swing.JButton();
        fastbackwardbutton = new javax.swing.JButton();
        soundbutton = new javax.swing.JToggleButton();
        nextbutton = new javax.swing.JButton();
        previousbutton = new javax.swing.JButton();
        stopbutton = new javax.swing.JButton();
        showplaylistbutton = new javax.swing.JButton();
        openplaylistbutton = new javax.swing.JButton();
        repeatbutton = new javax.swing.JButton();
        shufflebutton = new javax.swing.JButton();
        informationlabel = new javax.swing.JLabel();
        soundlabel = new javax.swing.JLabel();
        logo = new javax.swing.JLabel();
        controllabel = new javax.swing.JLabel();
        openlabel = new javax.swing.JLabel();
        playerlabel = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        menuflie = new javax.swing.JMenu();
        menu1 = new javax.swing.JMenuItem();
        menu2 = new javax.swing.JMenuItem();
        menu3 = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("CS PLAYER");
        setBackground(new java.awt.Color(255, 255, 255));
        setLocationByPlatform(true);
        setResizable(false);
  
        duration.setBounds(240, 140, 280, 20);
        jLayeredPane1.add(duration, javax.swing.JLayeredPane.DEFAULT_LAYER);
        
        volumeslider.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(MouseEvent evt) {
                volume();
            }
        });
        volumeslider.setBounds(420, 165, 100, 23);
        jLayeredPane1.add(volumeslider, javax.swing.JLayeredPane.DEFAULT_LAYER);
 
        pausebutton.setBorderPainted(false); 
        pausebutton.setContentAreaFilled(false); 
        pausebutton.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent evt) { 
            	System.out.println("pause"); 
            	pause();
            } 
        }); 
        pausebutton.setBounds(90, 30, 60, 40); 
        jLayeredPane1.add(pausebutton, javax.swing.JLayeredPane.DEFAULT_LAYER); 
 
        playbutton.setBorderPainted(false); 
        playbutton.setContentAreaFilled(false); 
        playbutton.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent evt) { 
            	System.out.println("play"); 
            	play();
            } 
        }); 
        playbutton.setBounds(80, 80, 80, 80); 
        jLayeredPane1.add(playbutton, javax.swing.JLayeredPane.DEFAULT_LAYER); 
 
        fastforwardbutton.setBorderPainted(false); 
        fastforwardbutton.setContentAreaFilled(false); 
        fastforwardbutton.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent evt) { 
            	System.out.println("fastforwardbutton");  
            	fastforward();
            } 
        }); 
        fastforwardbutton.setBounds(160, 50, 50, 70); 
        jLayeredPane1.add(fastforwardbutton, javax.swing.JLayeredPane.DEFAULT_LAYER); 
 
        fastbackwardbutton.setBorderPainted(false); 
        fastbackwardbutton.setContentAreaFilled(false); 
        fastbackwardbutton.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent evt) { 
            	System.out.println("fastbackward");  
            	fastbackward();
            } 
        }); 
        fastbackwardbutton.setBounds(33, 50, 50, 70); 
        jLayeredPane1.add(fastbackwardbutton, javax.swing.JLayeredPane.DEFAULT_LAYER); 
 
        soundbutton.setIcon(new javax.swing.ImageIcon(getClass().getResource("icons/sound.JPG"))); // NOI18N
        soundbutton.setBorderPainted(false);
        soundbutton.setContentAreaFilled(false);
        soundbutton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(MouseEvent evt) {
                soundbuttonMouseEntered();
            }
            public void mouseExited(MouseEvent evt) {
                soundbuttonMouseExited();
            }
        });
        soundbutton.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent evt) { 
            	System.out.println("sound"); 
            	sound();
            } 
        }); 
        soundbutton.setBounds(390, 160, 30, 30);
        jLayeredPane1.add(soundbutton, javax.swing.JLayeredPane.DEFAULT_LAYER); 
 
        nextbutton.setBorderPainted(false); 
        nextbutton.setContentAreaFilled(false); 
        nextbutton.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent evt) { 
            	System.out.println("next"); 
            	next();
            } 
        }); 
        nextbutton.setBounds(160, 120, 50, 70); 
        jLayeredPane1.add(nextbutton, javax.swing.JLayeredPane.DEFAULT_LAYER); 
 
        previousbutton.setBorderPainted(false); 
        previousbutton.setContentAreaFilled(false); 
        previousbutton.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent evt) { 
            	System.out.println("previous"); 
            	previous();
            } 
        }); 
        previousbutton.setBounds(33, 120, 50, 70); 
        jLayeredPane1.add(previousbutton, javax.swing.JLayeredPane.DEFAULT_LAYER); 
 
        stopbutton.setBorderPainted(false); 
        stopbutton.setContentAreaFilled(false); 
        stopbutton.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent evt) { 
            	System.out.println("stop");  
            	stop();
            } 
        }); 
        stopbutton.setBounds(90, 170, 70, 50); 
        jLayeredPane1.add(stopbutton, javax.swing.JLayeredPane.DEFAULT_LAYER); 
 
        showplaylistbutton.setIcon(new javax.swing.ImageIcon(getClass().getResource("icons/Playlist.gif"))); // NOI18N
        showplaylistbutton.setBorderPainted(false);
        showplaylistbutton.setContentAreaFilled(false);
        showplaylistbutton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                showplaylistbuttonMouseEntered();
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                showplaylistbuttonMouseExited();
            }
        }); 
        showplaylistbutton.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent evt) { 
            	System.out.println("playlist");  
            	managerAction();
            } 
        }); 
        showplaylistbutton.setBounds(270, 160, 30, 30);
        jLayeredPane1.add(showplaylistbutton, javax.swing.JLayeredPane.DEFAULT_LAYER); 
 
        openplaylistbutton.setIcon(new javax.swing.ImageIcon(getClass().getResource("icons/open.JPG"))); // NOI18N
        openplaylistbutton.setBorderPainted(false);
        openplaylistbutton.setContentAreaFilled(false);
        openplaylistbutton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                openplaylistbuttonMouseEntered();
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                openplaylistbuttonMouseExited();
            }
        });
        openplaylistbutton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                playlistAction();
            }
        });
        openplaylistbutton.setBounds(240, 160, 30, 30);
        jLayeredPane1.add(openplaylistbutton, javax.swing.JLayeredPane.DEFAULT_LAYER);
        
        repeatbutton.setIcon(new javax.swing.ImageIcon(getClass().getResource("icons/repeat.JPG"))); // NOI18N
        repeatbutton.setBorderPainted(false);
        repeatbutton.setContentAreaFilled(false);
        repeatbutton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                repeatbuttonMouseEntered();
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                repeatbuttonMouseExited();
            }
        });
        repeatbutton.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent evt) { 
            	System.out.println("repeat");  
            	repeat();
            } 
        }); 
        repeatbutton.setBounds(310, 160, 30, 30);
        jLayeredPane1.add(repeatbutton, javax.swing.JLayeredPane.DEFAULT_LAYER);
 
        shufflebutton.setIcon(new javax.swing.ImageIcon(getClass().getResource("icons/shuffle.JPG"))); // NOI18N
        shufflebutton.setBorderPainted(false);
        shufflebutton.setContentAreaFilled(false);
        shufflebutton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                shufflebuttonMouseEntered();
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                shufflebuttonMouseExited();
            }
        });
        shufflebutton.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent evt) { 
            	System.out.println("shuffle"); 
            	shuffle();
            } 
        }); 
        shufflebutton.setBounds(340, 160, 30, 30);
        jLayeredPane1.add(shufflebutton, javax.swing.JLayeredPane.DEFAULT_LAYER);
        informationlabel.setBounds(310, 60, 210, 80);
        jLayeredPane1.add(informationlabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        soundlabel.setBounds(400, 190, 70, 20);
        jLayeredPane1.add(soundlabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("icons/Logo.jpg"))); // NOI18N
        logo.setBounds(220, 50, 74, 80);
        jLayeredPane1.add(logo, javax.swing.JLayeredPane.DEFAULT_LAYER);
        controllabel.setBounds(300, 190, 110, 20);
        jLayeredPane1.add(controllabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        openlabel.setBounds(260, 190, 90, 20);
        jLayeredPane1.add(openlabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
 
        playerlabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("icons/cs player3.png"))); // NOI18N
        playerlabel.setBounds(0, 0, 540, 240);
        jLayeredPane1.add(playerlabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        menuflie.setText("FILE");
 

        menu1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        menu1.setText("Open File");
        menu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menu1ActionPerformed(evt);
            }
        });
        menuflie.add(menu1);

        menu2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, java.awt.event.InputEvent.CTRL_MASK));
        menu2.setText("Open Playlist Manager");
        menu2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menu2ActionPerformed(evt);
            }
        });
        menuflie.add(menu2);
        menuflie.addSeparator();
        menu3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        menu3.setText("Exit");
        menu3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menu3ActionPerformed(evt);
            }
        });
        menuflie.add(menu3);

        jMenuBar1.add(menuflie);

        jMenu1.setText("ABOUT");
        jMenu1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenu1ActionPerformed(evt);
            }
        });

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);


        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 539, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold> 
 
   
    


	protected void menu3ActionPerformed(ActionEvent evt) {
		System.exit(0);
		
	}

	protected void menu2ActionPerformed(ActionEvent evt) {
		managerAction();
		
	}

	protected void jMenu1ActionPerformed(MouseEvent evt) {
		JOptionPane.showMessageDialog(null,"About: CSPlayer \n" +
				"CS Music is a media player which is easy to use. \n" +
				"The player will contain all functions that usually a common media player has. \n" +
				"The CS Music supports the most popular music format (mp3). \n" +
				"Our CS Music will link to database for song information.\n\n" +
				"Project Advisor: Dr Dhanesh Ramachandram\n" +
				"Project Leader: Tan Chee Yong\n" +
				"Project Members:\n" +
				"Lim Heng Kuan\n" +
				"Nur Ain Binti Jais\n" +
				"Che Hairina Binti Che Mohamad\n" +
				"Group: CAT200 Group 15 / 2010");	
	}

	private void menu1ActionPerformed(ActionEvent evt) {
		
		playlistAction();
	}
	public void errorMessage(String s) {
		JOptionPane.showMessageDialog(null, s, "Error", JOptionPane.ERROR_MESSAGE);
	}
    
    private void managerAction() {
		if(playlist != null) {
			PlaylistManage manager = new PlaylistManage(this);
			manager.setSize(400, 300);
			manager.setLocation(getX()+ this.getWidth(),this.getY());
			manager.setVisible(true);
		}
	}
    private void playlistAction() {
		JFileChooser fileChooser = new JFileChooser(Setting.getPlaylistDirectory());
		fileChooser.setMultiSelectionEnabled(false);
		fileChooser.addChoosableFileFilter(new PlaylistFilter());
		if(fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
			duration.setString("Loading Playlist...");
			playlist = new Playlist();
			try {
				File playlistFile = fileChooser.getSelectedFile();
				playlist.loadFromFile(playlistFile);
				Setting.setPlaylistDirectory(playlistFile.getParent());
				Setting.setPlaylistFile(playlistFile.getName());
			}
			catch(IOException ex) {
				errorMessage(ex.getMessage());
			}
			catch(PlaylistException ex) {
				errorMessage(ex.getMessage());
			}
			duration.setString("");
		}
	}

	public Playlist getPlaylist() {
		return playlist;							//public class Playlist extends LinkedList
	}
	public void setPlaylist(Playlist p) {
		playlist = p;								//public class Playlist extends LinkedList
	}
	public Player getPlayer() {
		return player;								//import javax.media.Player;
	}
	public void play() {
		MP3File mp3File = null;						//import helliker.id3.MP3File;
		int position = Setting.getPlaylistPosition(); //public class Settings Line29
		try {
			mp3File = (MP3File) playlist.get(position);
			fileTitle = mp3File.getTitle();	
			fileArtist = mp3File.getArtist();
			fileAlbum = mp3File.getAlbum();
			//private String fileTitle = "";
			//public class MP3File implements Comparable Line665
		}
		catch(ID3v2FormatException ex) {
			//in helliker ...public class ID3v2FormatException extends Exception
			errorMessage(ex.getMessage());
		}
		if(player == null) {					//import javax.media.Player;
			try {
				File file = new File(mp3File.getPath());
				MediaLocator mediaLocator = new MediaLocator(file.toURL());
				//import javax.media.MediaLocator;
				
				player = Manager.createPlayer(mediaLocator);		//import javax.media.Player;
				//import javax.media.Manager;
				
				player.addControllerListener(this);			////import javax.media.Player;
				duration.setString("Realizing...");		//import javax.swing.JProgressBar;
				player.realize();							//import javax.media.Player;
			}
			catch(MalformedURLException ex) {
				errorMessage(ex.getMessage());
			}
			catch(NoPlayerException ex) {
				errorMessage(ex.getMessage());
			}
			catch(IOException ex) {
				errorMessage(ex.getMessage());
			}
		}
		else {		
			player.setRate(1.0f); //Normal again
			player.start();					//import javax.media.Player;
			informationlabel.setText("<html>Playing " + fileTitle + "<br>Artist : "+fileArtist + "<br>Album : "+fileAlbum); //private String fileTitle = "";
			//import javax.swing.JProgressBar;
		}
	}
	
	public void fastforward() {
			if(player != null) { 				//import javax.media.Player;
				MP3File mp3File = null;
				int position = Setting.getPlaylistPosition(); //public class Settings Line29
				try {
					mp3File = (MP3File) playlist.get(position);
					fileTitle = mp3File.getTitle();			//private String fileTitle = "";
					//public class MP3File implements Comparable Line665
					
					//import javax.swing.JProgressBar;
				}
				catch(ID3v2FormatException ex) {
					//in helliker.... public class ID3v2FormatException extends Exception
					errorMessage(ex.getMessage());
				}
				player.stop();	
				player.setRate(2.0f);
				player.start();	
			}
	}
	
	public void fastbackward() {
		if(player != null) { 				//import javax.media.Player;
			MP3File mp3File = null;
			int position = Setting.getPlaylistPosition(); //public class Settings Line29
			try {
				mp3File = (MP3File) playlist.get(position);
				fileTitle = mp3File.getTitle();			//private String fileTitle = "";
				//public class MP3File implements Comparable Line665
				
				//import javax.swing.JProgressBar;
			}
			catch(ID3v2FormatException ex) {
				//in helliker.... public class ID3v2FormatException extends Exception
				errorMessage(ex.getMessage());
			}
			player.stop();	
			player.setRate(0.5f);
			player.start();	
		}
}
	
	private void pause() {
		if(player != null) { 				//import javax.media.Player;
			MP3File mp3File = null;
			int position = Setting.getPlaylistPosition(); //public class Settings Line29
			try {
				mp3File = (MP3File) playlist.get(position);
				fileTitle = mp3File.getTitle();			//private String fileTitle = "";
				//public class MP3File implements Comparable Line665
				
				informationlabel.setText( "Paused" );
				//import javax.swing.JProgressBar;
			}
			catch(ID3v2FormatException ex) {
				//in helliker.... public class ID3v2FormatException extends Exception
				errorMessage(ex.getMessage());
			}
			player.stop();				//import javax.media.Player;
		}
	}
	public void stop() {
		if(player != null) {			//import javax.media.Player;
			player.removeControllerListener(this);
			player.stop();
			player.close();
			player = null;
		}
		if(playThread != null) {		//private Thread playThread
			playThread = null;
		}
		duration.setValue(0);
		duration.setString("");
		informationlabel.setText("Stop");
	}
	private void previous() {
		int position = Setting.getPlaylistPosition();//public class Settings Line29
		position--;
		if(position < 0)
			position = playlist.size()-1;
		Setting.setPlaylistPosition(position);		//public class Settings Line32
		if(player != null)							//import javax.media.Player;
			stop();
		play();
	}
	private void next() {
		int position = Setting.getPlaylistPosition();//public class Settings Line29
		if (sh)
			position = random.nextInt(playlist.size());
		else{
		position++;
		if(position >= playlist.size()) {
			position = 0;
			if (!r)
			{
			Setting.setPlaylistPosition(position);	//public class Settings Line32
			stop();
			return;
			}
		}
		}
		Setting.setPlaylistPosition(position);		//public class Settings Line32
		if(player != null)							//import javax.media.Player;
			stop();
		play();
	}
	
	
	public void controllerUpdate(ControllerEvent ev) {
		if(ev instanceof RealizeCompleteEvent) {
			player.prefetch();							//import javax.media.Player;
			duration.setString("Prefetching...");
		}
		if(ev instanceof PrefetchCompleteEvent) {
			Time time = player.getDuration();
			duration.setMaximum((int) time.getSeconds());
			informationlabel.setText("<html>Playing " + fileTitle + "<br>Artist : "+fileArtist + "<br>Album : "+fileAlbum);
			playThread = new Thread(this);
			playThread.start();
			player.getGainControl().setLevel(1);
			player.start();
		}
		if(ev instanceof EndOfMediaEvent) {
			player.removeControllerListener(this);
			player.stop();
			player.close();
			player = null;
			if(playThread != null) {
				playThread = null;
			}
			duration.setValue(0);
			next();
		}
	}
	public void mouseDragged(MouseEvent ev) {
		Point loc = getLocation();
		Point clk = ev.getPoint();
		Point pt = new Point(loc.x + clk.x,loc.y + clk.y);
		setLocation(pt);	
	}
	public void mouseMoved(MouseEvent ev) {}
	public void run() {
		while(playThread != null) {
			if(player != null) {
				Time time = player.getMediaTime();
				duration.setValue((int) time.getSeconds());
				try {
					playThread.sleep(500);
				}
				catch(InterruptedException ex) {}
			}
		}
	}
	class PlaylistFilter extends FileFilter{
		public boolean accept(File file) {
			if(file.isDirectory())
				return true;
			String s = file.getName().toLowerCase();
			if (s.endsWith(".m3u"))
				return true;
			return false;
		}
		public String getDescription() {
			return "Playlist Files";
		}
			
	}
	

	public void sound(){
		if (s){
            s=false;
            soundbutton.setIcon(new javax.swing.ImageIcon(getClass().getResource("icons/sound1.JPG")));
            Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
            for (int i = 0; i < mixerInfos.length; i++) {

            System.out.println("AudioSystem info Name:"+mixerInfos[i].toString());
            Mixer mixer = AudioSystem.getMixer(mixerInfos[i]);

            Line.Info[] targetLineInfos = mixer.getTargetLineInfo();
            System.out.println("target infos : " +
            targetLineInfos.length);
            for (int j = 0; j < targetLineInfos.length; j++) {
            setMute(targetLineInfos[j]);
            }
            }
        }
        else{
            s=true;
            soundbutton.setIcon(new javax.swing.ImageIcon(getClass().getResource("icons/sound.JPG")));
            Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
            for (int i = 0; i < mixerInfos.length; i++) {

            System.out.println("AudioSystem info Name:"+mixerInfos[i].toString());
            Mixer mixer = AudioSystem.getMixer(mixerInfos[i]);

            Line.Info[] targetLineInfos = mixer.getTargetLineInfo();
            System.out.println("target infos : " +
            targetLineInfos.length);
            for (int j = 0; j < targetLineInfos.length; j++) {
            setMute(targetLineInfos[j]);
            }
            }
        }
	}
	
	public void repeat(){
		if (r){
            r=false;
            repeatbutton.setIcon(new javax.swing.ImageIcon(getClass().getResource("icons/repeat.JPG")));
            System.out.println("repeat off");
        }
        else{
            r=true;
            repeatbutton.setIcon(new javax.swing.ImageIcon(getClass().getResource("icons/repeat1.JPG")));
            System.out.println("repeat on");
        }
	}
	
	public void shuffle(){
		if (sh){
            sh=false;
            shufflebutton.setIcon(new javax.swing.ImageIcon(getClass().getResource("icons/shuffle.JPG")));
            System.out.println("shuffle off");
        }
        else{
            sh=true;
            shufflebutton.setIcon(new javax.swing.ImageIcon(getClass().getResource("icons/shuffle1.JPG")));
            System.out.println("shuffle on");
        }
	}
	
	public void volume() {
		Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
        for (int i = 0; i < mixerInfos.length; i++) {

        System.out.println("AudioSystem info Name:"+mixerInfos[i].toString());
        Mixer mixer = AudioSystem.getMixer(mixerInfos[i]);

        Line.Info[] targetLineInfos = mixer.getTargetLineInfo();
        System.out.println("target infos : " +
        targetLineInfos.length);
        for (int j = 0; j < targetLineInfos.length; j++) {
        setVolume(targetLineInfos[j]);
        }
        }
    }
	
	
	public boolean setMute(Line.Info lineInfo) {
    	try {
    	System.out.println(lineInfo);
    	Line line = AudioSystem.getLine(lineInfo);
    	System.out.println("open " + line.getLineInfo());
    	line.open();

    	BooleanControl muteControl
    	=(BooleanControl)line.getControl(BooleanControl.Type.MUTE);
    	System.out.println("mute " + muteControl);
    	if (s)
    		muteControl.setValue(false);
    	else
    		muteControl.setValue(true);
    		
    	line.close();
    	} catch (Exception e) {
    	System.out.println(e);
    	return false;
    	}
    	return true;
    	}
    
    
    public boolean setVolume(Line.Info lineInfo) {
    	try {
    	System.out.println(lineInfo);
    	Line line = AudioSystem.getLine(lineInfo);
    	System.out.println("open " + line.getLineInfo());
    	line.open();  		
    	FloatControl control =
    	(FloatControl)line.getControl(FloatControl.Type.VOLUME);
//    	System.out.println("VOLUME :       "+volumeslider.getValue());
    	control.setValue(volumeslider.getValue());
    	line.close();
    	} catch (Exception e) {
    	System.out.println(e);
    	return false;
    	}
    	return true;
    	}


////////    To Set label /////////////////////////
	public void soundbuttonMouseEntered(){
		if(s){
            soundlabel.setText("Turn off");
        }
        
        else{
            soundlabel.setText("Turn on");
        }
	}
	
	public void soundbuttonMouseExited(){
		soundlabel.setText(null);
	}
	
	public void shufflebuttonMouseEntered() {                                           
        if(sh){
            controllabel.setText("Turn off shuffle");
        }

        else{
            controllabel.setText("Turn on shuffle");
        }
    }
	
	public void shufflebuttonMouseExited() {                                          
        controllabel.setText(null);
    }                                         

	public void repeatbuttonMouseEntered() {                                          
        if(r){
            controllabel.setText("Turn off repeat");
        }

        else{
            controllabel.setText("Turn on repeat");
        }
    }                                         

    public void repeatbuttonMouseExited() {                                         
        controllabel.setText(null);
    }                                        

    public void showplaylistbuttonMouseEntered() {                                                
        openlabel.setText("Playlist");
    }                                               

    public void showplaylistbuttonMouseExited() {                                               
        openlabel.setText(null);
    }                                              

    public void openplaylistbuttonMouseEntered() {                                                
        openlabel.setText("Open file");
    }                                               

    public void openplaylistbuttonMouseExited() {                                               
        openlabel.setText(null);
    }
    


/////////////////////////////////////////////////////
	
    public static void main(String args[]) { 
        java.awt.EventQueue.invokeLater(new Runnable() { 
            public void run() { 
                new CSPlayer().setVisible(true); 
            } 
        }); 
    } 
 
    // Variables declaration - do not modify 
    private javax.swing.JLabel controllabel;
    private javax.swing.JProgressBar duration;
    private javax.swing.JButton fastbackwardbutton;
    private javax.swing.JButton fastforwardbutton;
    private javax.swing.JLabel informationlabel;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JLabel logo;
    private javax.swing.JMenuItem menu1;
    private javax.swing.JMenuItem menu2;
    private javax.swing.JMenuItem menu3;
    private javax.swing.JMenu menuflie;
    private javax.swing.JButton nextbutton;
    private javax.swing.JLabel openlabel;
    private javax.swing.JButton openplaylistbutton;
    private javax.swing.JButton pausebutton;
    private javax.swing.JButton playbutton;
    private javax.swing.JLabel playerlabel;
    private javax.swing.JButton previousbutton;
    private javax.swing.JButton repeatbutton;
    private javax.swing.JButton showplaylistbutton;
    private javax.swing.JButton shufflebutton;
    private javax.swing.JToggleButton soundbutton;
    private javax.swing.JLabel soundlabel;
    private javax.swing.JButton stopbutton;
    private javax.swing.JSlider volumeslider;
    // End of variables declaration 
 
} 


